<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2015/7/31
 * Time: 11:04
 */

namespace p5\objects;


class RandomUtils
{
    public static function randomNumbers($len = 6)
    {
        $seeds = [0, 1, 2, 3, 5, 6, 7, 8, 9];
        $string = "";
        for ($i = 0; $i < $len; $i++) {
            $index = rand(0, 8);
            $string .= $seeds[$index];
        }
        return $string;
    }


    static public function  randomString($length)
    {
        $str = array_merge(range(0, 9), range('a', 'z'), range('A', 'Z'));
        shuffle($str);
        $str = implode('', array_slice($str, 0, $length));
        return $str;
    }

    public static function randomPassword($length = 6)
    {
        // 密码字符集，可任意添加你需要的字符
        $chars = 'abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789';
        $password = '';
        for ($i = 0; $i < $length; $i++) {
            // 这里提供两种字符获取方式
            // 第一种是使用 substr 截取$chars中的任意一位字符；
            // 第二种是取字符数组 $chars 的任意元素
            // $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
            $password .= $chars[mt_rand(0, strlen($chars) - 1)];
        }
        return $password;
    }

}